package com.share.tool.attack.async;

import com.share.common.utils.ThreadUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

public class Jdk8Feature {
    public static void main(String[] args) {
        testNone();
//        testExecutor();
//        testCompletableFuture();
    }

    public static void testNone() {
        List<CompletableFuture<Void>> futureList = new ArrayList<>();
        try {
            CompletableFuture.allOf(futureList.toArray(new CompletableFuture[futureList.size()])).get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("testNone执行结束");
    }

    public static void testCompletableFuture() {
        final int size = 100;
        Executor executor = ThreadUtil.getExecutor();
        final Map<String, Object> resultMap = new ConcurrentHashMap<String, Object>();
        List<CompletableFuture<Void>> futureList = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            String key = "" + i;
            CompletableFuture<Void> futureTemp = CompletableFuture.supplyAsync(() -> {
                int jj1 = 0;
                return "fdfd";
            }, executor).exceptionally(e -> {
                return "fdf";
            }).thenAccept(r -> {
                int jj3 = 0;
                resultMap.put(key, r);
            });

            futureList.add(futureTemp);
        }

        System.out.println("testCompletableFuture1执行结束=" + resultMap.size());

        futureList.stream().forEach(e -> {
            try {
                e.get();
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            } catch (ExecutionException executionException) {
                executionException.printStackTrace();
            }
        });
        System.out.println("testCompletableFuture2执行结束=" + resultMap.size());
        CompletableFuture<Void> result = CompletableFuture.allOf(futureList.toArray(new CompletableFuture[futureList.size()]));
        try {
            result.get();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("testCompletableFuture3执行结束=" + resultMap.size());
        ThreadUtil.shutdown();
    }

    public static void testExecutor() {
        final int size = 100;
        CountDownLatch countDownLatch = new CountDownLatch(size);
        final Map<String, Object> resultMap = new ConcurrentHashMap<String, Object>();
        for (int i = 0; i < size; i++) {
            String key = "" + i;
            ThreadUtil.submit(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    resultMap.put(key, "fd");
//                    System.out.println("--" + key);
                    countDownLatch.countDown();
                    return "null";
                }
            });
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("testExecutor执行结束=" + resultMap.size());
        ThreadUtil.shutdown();
    }
}
